<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var solveNQueens = function (n) {
      let res = []
      let path = []
      const row = new Array(n).fill('.')
      backtracking(0)
      return res
      function backtracking() {
        if (path.length === n) {
          res.push([...path])
          return
        }
        for (let i = 0; i < n; i++) {
          if (!isVilidate(i)) {
            continue;
          }
          const deepRow = [...row]
          deepRow[i] = 'Q'
          path.push(deepRow.join(''))
          backtracking()
          path.pop()
        }
      }
      function isVilidate(i) {
        if (path.length === 0) {
          return true
        }
        const vilidate = path.every((item, index) => {
          const rowIndex = item.indexOf('Q')
          const over = path.length - index
          return i !== rowIndex && i !== rowIndex + over && i !== rowIndex - over
        })
        return vilidate
      };
    }

    console.log(solveNQueens(4));
  </script>
</body>

</html>